<template>
  <audio class="audio" loop controls autoplay ref="audio">
    <source src="http://biaodan.fdpet.cn/bgm.mp3" type="audio/mp3" />
  </audio>
  <div class="bgm" :class="{ active: isActive }" @click="play"></div>
</template>

<script>
import { defineComponent, ref, onMounted } from "vue";

export default defineComponent({
  setup() {
    const audio = ref(null);
    const isActive = ref(true);
    const play = () => {
      if (audio.value.paused) {
        audio.value.play();
        isActive.value = true;
      } else {
        audio.value.pause();
        isActive.value = false;
      }
    };
    onMounted(async () => {
      // userGetluckRecord();
      console.log(audio.value);
      //   audio.value.play()
      setTimeout(() => {
        //   audio.value.pause()
        audio.value.onended = function () {
          audio.value.load();
          isActive.value = true;
        };
      }, 300);
      audio.value.onended = function () {
        audio.value.load();
        isActive.value = true;
      };
    });
    return {
      audio,
      isActive,
      play,
    };
  },
});
</script>

<style lang="scss" scoped>
.audio {
  position: absolute;
  top: -100px;
}
.bgm {
  position: fixed;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  animation: xz 1s;
  background-image: url("http://biaodan.fdpet.cn/bgm.jpg");
  background-size: 120% 120%;
  background-position: center;
  z-index: 3;
}

.active {
  animation: xz 1s linear infinite;
}
@keyframes xz {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>